/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 *Truth: I Believe in Spring Brother~
 */
package electsys;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.apache.commons.httpclient.NameValuePair;

/**
 *
 * @author t
 * Created 2010-7-21  9:23:19
 * Last Modified 2010-7-21  9:23:19
 *
 */
public class WebGrab {

    public Client myClient = null;
    public boolean login = false;

    public WebGrab() {
    }

    public WebGrab(Client client) {
        setClient(client);
    }

    public void setClient(Client client) {
        myClient = client;
    }

    public boolean login(Client client, String username, String password) {
        if (client == null) {
            client = myClient;
        }
        if (client == null) {
            Logger.getLogger(WebGrab.class.getName()).info("Client ERROR!");
            return false;
        }
        String indexPage = client.getHtml(MyConst.ROOT);
        String viewstate = MyUtil.getVIEWSTATE(indexPage);
        String eventvalidation = MyUtil.getEVENTVALIDATION(indexPage);
        //System.out.println(viewstate);
        //System.out.println(eventvalidation);
        NameValuePair[] nvps = new NameValuePair[]{
            new NameValuePair(MyConst.PAGE_VIEWSTATE_NAME, viewstate),
            new NameValuePair(MyConst.PAGE_EVENTVALIDATION_NAME, eventvalidation),
            new NameValuePair(MyConst.LOGIN_USERNAME_NAME, username),
            new NameValuePair(MyConst.LOGIN_PASSWORD_NAME, password),
            new NameValuePair(MyConst.LOGIN_TYPE_NAME, MyConst.LOGIN_TYPE_VALUE),
            new NameValuePair(MyConst.LOGIN_BUTTON_NAME, MyConst.LOGIN_BUTTON_VALUE)
        };
        String ret = client.postHtml(MyConst.ROOT + MyConst.LOGIN_PAGE, nvps);
        //   System.out.println("ret0:" + ret);
        ret = client.getHtml(MyConst.ROOT + MyConst.REDIRECT1);
        //    System.out.println("ret1:" + ret);
        // It's OK here. doGet() does auto-redirect, so we donot need the REDIRECT2

        //    ret = client.getHtml(MyConst.ROOT + MyConst.REDIRECT2);
        //    System.out.println("ret2:" + ret);

        //ret = client.getHtml("http://electsys0.sjtu.edu.cn/edu/student/elect/electResultOuter.aspx");
        //System.out.println("MainPage :" + ret);
        if (MyUtil.getTitle(ret.toLowerCase()).equals(MyConst.LOGIN_SUCCESS_TITLE)) {
            this.login = true;
            return true;
        } else {
            this.login = false;
            return false;
        }
    }

    public List<String> getLessonXNOptions(Client client) {
        if (client == null) {
            client = myClient;
        }
        if (client == null) {
            Logger.getLogger(WebGrab.class.getName()).info("Client ERROR!");
            return null;
        }
        String ret = client.getHtml(MyConst.ROOT + MyConst.LESSONTABLE_PAGE);
        //System.out.println(ret);
        List<String> xns = MyUtil.getOptionsFromForm(ret);
        return xns;
    }

    public List<String> getExamXNOptions(Client client) {
        if (client == null) {
            client = myClient;
        }
        if (client == null) {
            Logger.getLogger(WebGrab.class.getName()).info("Client ERROR!");
            return null;
        }
        String ret = client.getHtml(MyConst.ROOT + MyConst.EXAM_PAGE);
        //System.out.println(ret);
        List<String> xns = MyUtil.getOptionsFromForm(ret);
        return xns;
    }

    public LessonTable getLessonTb(Client client, String xn, String xq) {
        if (client == null) {
            client = myClient;
        }
        if (client == null) {
            Logger.getLogger(WebGrab.class.getName()).info("Client ERROR!");
            return null;
        }
        LessonTable lessontb = new LessonTable();
        String ltb = client.getHtml(MyConst.ROOT + MyConst.LESSONTABLE_PAGE);
        String viewstate = MyUtil.getVIEWSTATE(ltb);
        String eventvalidation = MyUtil.getEVENTVALIDATION(ltb);
        //System.out.println(ltb);
        //System.out.println(viewstate);
        //System.out.println(eventvalidation);
        //System.out.println(MyUtil.getValueFromForm(MyUtil.getSelectedOption(MyUtil.getLTBXNOptions(ltb))));
        //System.out.println(MyUtil.getValueFromForm(MyUtil.getSelectedOption(MyUtil.getLTBXQOptions(ltb))));
        NameValuePair[] nvps = new NameValuePair[]{
            new NameValuePair(MyConst.PAGE_VIEWSTATE_NAME, viewstate),
            new NameValuePair(MyConst.PAGE_EVENTVALIDATION_NAME, eventvalidation),
            new NameValuePair(MyConst.LTB_DPXN_NAME, xn),
            new NameValuePair(MyConst.LTB_DPXQ_NAME, xq),
            new NameValuePair(MyConst.LTB_BUTTON_NAME, MyConst.LTB_BUTTON_VALUE)
        };
        String ret = client.postHtml(MyConst.ROOT + MyConst.LESSONTABLE_PAGE, nvps);
        //System.out.println(ret);

        if (!MyUtil.getTitle(ret).equals(MyConst.LTB_SUCCESS_TITLE)) {
            Logger.getLogger(WebGrab.class.getName()).info("ERROR: List Lesson Table Failed..");
            return null;
        }
        addLessonsFromStr(ret, lessontb);
        return lessontb;
    }

    public List<ExamInfo> getExams(Client client, String xn, String xq) {
        if (client == null) {
            client = myClient;
        }
        if (client == null) {
            Logger.getLogger(WebGrab.class.getName()).info("Client ERROR!");
            return null;
        }
        List<ExamInfo> eis = new ArrayList();
        String exam = client.getHtml(MyConst.ROOT + MyConst.EXAM_PAGE);
        String viewstate = MyUtil.getVIEWSTATE(exam);
        String eventvalidation = MyUtil.getEVENTVALIDATION(exam);
        //System.out.println(ltb);
        //System.out.println(viewstate);
        //System.out.println(eventvalidation);
        //System.out.println(MyUtil.getValueFromForm(MyUtil.getSelectedOption(MyUtil.getLTBXNOptions(ltb))));
        //System.out.println(MyUtil.getValueFromForm(MyUtil.getSelectedOption(MyUtil.getLTBXQOptions(ltb))));
        NameValuePair[] nvps = new NameValuePair[]{
            new NameValuePair(MyConst.PAGE_VIEWSTATE_NAME, viewstate),
            new NameValuePair(MyConst.PAGE_EVENTVALIDATION_NAME, eventvalidation),
            new NameValuePair(MyConst.EXAM_DPXN_NAME, xn),
            new NameValuePair(MyConst.EXAM_DPXQ_NAME, xq),
            new NameValuePair(MyConst.EXAM_BUTTON_NAME, MyConst.EXAM_BUTTON_VALUE)
        };
        String ret = client.postHtml(MyConst.ROOT + MyConst.EXAM_PAGE, nvps);
        //System.out.println(ret);

        if (!MyUtil.getTitle(ret).equals(MyConst.EXAM_SUCCESS_TITLE)) {
            Logger.getLogger(WebGrab.class.getName()).info("ERROR: List Exam Info failed..");
            return null;
        }
        addExamsFromStr(ret, eis);
        return eis;
    }

    public void addLessonsFromStr(String page, LessonTable lessontb) {
        String tmp = MyUtil.getLessonTable(page);
        List<String> trs = MyUtil.getTRs(tmp);
        if (trs == null) {
            return;
        }
        for (int i = 1; i < trs.size(); i++) {
            List<String> tds = MyUtil.getTDs(trs.get(i));
            for (int j = 1; j < tds.size(); j++) {
                List<String> infos = MyUtil.getLessonInfos(tds.get(j));
                for (int k = 0; k < infos.size(); k++) {
                    //System.out.println(infos.get(k));
                    LessonInfo li = new LessonInfo();
                    li.id = MyUtil.getLessonID(infos.get(k));
                    li.name = MyUtil.getLessonName(infos.get(k));
                    li.classroom = MyUtil.getLessonLocation(infos.get(k));
                    li.weeks = MyUtil.getLessonWeek(infos.get(k));
                    li.dayOfWeek = j - 1;
                    //li.weeks = MyUtil.getLessonWeek(infos.get(k));
                    li.startTime = MyUtil.getLessonStartTime(trs.get(i));
                    li.endTime = MyUtil.getLessonEndTime(trs.get(i), tds.get(j));
                    li.week = MyUtil.getLessonWeekStr(infos.get(k));
                    //System.out.println(li.name + " "+ li.weeks.size());
                    lessontb.addCourse(li);
                }
            }
        }
    }

    public void addExamsFromStr(String page, List<ExamInfo> eis) {
        String tmp = MyUtil.getExamTable(page);
        //System.out.println(tmp);
        List<String> trs = MyUtil.getTRs(tmp);
        //System.out.println(trs.size());
        if (trs==null) {
            return ;
        }
        for ( int i = 1; i < trs.size(); i++) {
            List<String> tds = MyUtil.getTDs(trs.get(i));
                ExamInfo ei = new ExamInfo();
                ei.name = MyUtil.getValueFromTD(tds.get(0)).trim();
                ei.notes = MyUtil.getValueFromTD(tds.get(1));
                ei.classroom = MyUtil.getValueFromTD(tds.get(3));
                ei.teacher = MyUtil.getValueFromTD(tds.get(4));
                ei.startTime = MyUtil.getExamStartTime(MyUtil.getValueFromTD(tds.get(2)));
                ei.endTime = MyUtil.getExamEndTime(MyUtil.getValueFromTD(tds.get(2)));
                eis.add(ei);
                //System.out.println(ei.getMainInfo());
        }
    }


}
